package cn.com.bob.alipay.web.utils;


import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;

import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Author Bob
 * @Version：1.0
 */
@Component
public class AlipayUtils {
    @Value("${alipay.url}")
    private String url;
    @Value("${alipay.appId}")
    private String appId;
    @Value("${alipay.privateKey}")
    private String privateKey;
    @Value("${alipay.publicKey}")
    private String publicKey;
    @Value("${alipay.notifyUrl}")
    private String notifyUrl;
    @Value("${alipay.returnUrl}")
    private String returnUrl;


    public  String pay(String id,String price,String title){
        AlipayClient alipayClient = new DefaultAlipayClient(url,appId,privateKey,"json","utf-8",publicKey,"RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //异步接收地址，仅支持http/https，公网可访问
        request.setNotifyUrl(notifyUrl);
        //同步跳转地址，仅支持http/https
        request.setReturnUrl(returnUrl);
        /******必传参数******/
        JSONObject bizContent = new JSONObject();
        //商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", id);
        //支付金额，最小值0.01元
        bizContent.put("total_amount", price);
        //订单标题，不可使用特殊符号
        bizContent.put("subject", title);
        //电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");


        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = null;
        String form = null;
        try {
            response = alipayClient.pageExecute(request);

            form = response.getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return form;
    }

    public String query(String id){
        AlipayClient alipayClient = new DefaultAlipayClient(url,appId,privateKey,"json","utf-8","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm8GUc8fP9p+YDLgVeGOEzAdB1oIGxzejkNmSfhqhvy0gbSbN+8WA1aNHzlZFFIheSDUGCyP+Y3CFtp5t/xfsMUHNOpDp7XA8oE1pzUsH6oYnEWfSNepr5hw2vFnhyP/8riPpWitjPjwj+9sSN9QCCkT34UUCxwJDoGNYAhxRvQLaH6p3oipYKCKUXh//EgYj/yj9/LCJK+KQX+HKmdh4dIWCCeHMI2myMzioGS/8Q9MejiE1IQqVJHRvhmKqQ4cx5ErU+1S6wRK7yDctoMZmIf1rX2QxFggl8GuSESCDaqmRVHPLYj4bhRFWij5/HDJvLZDqb4jgy+xn5FAYZd/RLwIDAQAB","RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body = null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }

        return body;
    }

    public String refund(String trade_no,String price,String id){
        AlipayClient alipayClient = new DefaultAlipayClient(url,appId,privateKey,"json","utf-8","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm8GUc8fP9p+YDLgVeGOEzAdB1oIGxzejkNmSfhqhvy0gbSbN+8WA1aNHzlZFFIheSDUGCyP+Y3CFtp5t/xfsMUHNOpDp7XA8oE1pzUsH6oYnEWfSNepr5hw2vFnhyP/8riPpWitjPjwj+9sSN9QCCkT34UUCxwJDoGNYAhxRvQLaH6p3oipYKCKUXh//EgYj/yj9/LCJK+KQX+HKmdh4dIWCCeHMI2myMzioGS/8Q9MejiE1IQqVJHRvhmKqQ4cx5ErU+1S6wRK7yDctoMZmIf1rX2QxFggl8GuSESCDaqmRVHPLYj4bhRFWij5/HDJvLZDqb4jgy+xn5FAYZd/RLwIDAQAB","RSA2");
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("trade_no",trade_no);
        bizContent.put("refund_amount", price);
        bizContent.put("out_request_no",id);
        //bizContent.put("refund_status",refund_status);

        //// 返回参数选项，按需传入
        //JSONArray queryOptions = new JSONArray();
        //queryOptions.add("refund_detail_item_list");
        //bizContent.put("query_options", queryOptions);

        request.setBizContent(bizContent.toString());
        AlipayTradeRefundResponse response = null;
        String body = null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if(response.isSuccess()){
            //System.out.println("调用成功");
            System.out.println("退款成功");
        } else {
            System.out.println("退款失败");
        }
        return body;
    }

    public void closeOrder(String id){
        AlipayClient alipayClient = new DefaultAlipayClient(url,appId,privateKey,"json","utf-8","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm8GUc8fP9p+YDLgVeGOEzAdB1oIGxzejkNmSfhqhvy0gbSbN+8WA1aNHzlZFFIheSDUGCyP+Y3CFtp5t/xfsMUHNOpDp7XA8oE1pzUsH6oYnEWfSNepr5hw2vFnhyP/8riPpWitjPjwj+9sSN9QCCkT34UUCxwJDoGNYAhxRvQLaH6p3oipYKCKUXh//EgYj/yj9/LCJK+KQX+HKmdh4dIWCCeHMI2myMzioGS/8Q9MejiE1IQqVJHRvhmKqQ4cx5ErU+1S6wRK7yDctoMZmIf1rX2QxFggl8GuSESCDaqmRVHPLYj4bhRFWij5/HDJvLZDqb4jgy+xn5FAYZd/RLwIDAQAB","RSA2");
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeCloseResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if(response.isSuccess()){
            System.out.println("订单取消成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
